/*************************************************************/
/*final_data_builder.do contains all STATA commands needed to go from raw data to
final dataset for "Climate Risk and Preferences over the Size of Government." 

First run the python code all_gis_commands.py which uses ArcGIS to construct the relevant fire-block group measures which form the independent variable. The python script also copies the raw data (available on Harvard Dataverse, see readme file) into a temp folder. Intermediate files are stored in the temp folder, which is deleted at the end (remove this line if you'd like to examine intermediate files). 

If you just want to reconstruct the voter data, make sure the raw data files are copied to a single folder, set the filepath accordingly, and run only sections 1-3 of the code (annotated below). Code divided into 7 sections for clarity.
MC 11302022
*/
*************************************************************
clear all

*update filepath accordingly if needed. Use ctrl+H "replace all" to update all instances of filepath definition
local filepath="temp\"

/*************************************************************/
/*Section 1. Collect voter registration and voter files from Statewide Database,
import to STATA, import precinct to block group crosswalks from Statewide Database, merge voter and registration files, use crosswalks to convert precincts to consistent Census geographies
MC 11302022
*/
*************************************************************


forvalues i=2008(2)2018 {

if `i'<2010 {
local num=`i'-2000
local j= "0`num'"
}
if `i'>=2010 {
local num=`i'-2000
local j= "`num'"
}

*these are the raw registration and voter files from Statewide Database
local filename_reg="state_g`j'_registration_by_g`j'_srprec"
local filename_voters="state_g`j'_voters_by_g`j'_srprec"
display "`filename'"

import delimited "`filepath'\`filename_voters'.csv", clear case(lower)
foreach var of varlist totreg_r-oreg9g {
rename `var' v_`var'
}

save "`filepath'\voters`j'.dta", replace

import delimited "`filepath'\`filename_reg'.csv", clear case(lower)
merge m:1 srprec_key using "`filepath'\voters`j'.dta"
drop _merge
save "`filepath'\reg_voters`j'.dta", replace

if `i'>2010 {
local crosswalk_name="state_g`j'_sr_blk_map"
}

if `i'<2010 {
local crosswalk_name="state_g`j'_2011blk_by_g`j'_sr"
}

if `i'==2010 {
local crosswalk_name="state_g10_2011blk_by_g10_sr"
}
import delimited "`filepath'\`crosswalk_name'.csv", clear case(lower)
merge m:1 srprec_key using "`filepath'\reg_voters`j'.dta"

drop if _merge!=3
drop _merge

save "`filepath'\`j'reg.dta", replace
erase "`filepath'\reg_voters`j'.dta"
erase "`filepath'\voters`j'.dta"
}

*Need to import 2002-2006 separately because these files are only available for registration precinct (rgprec), not voting precinct (srprec) which is the unit of analysis for the voting data
forvalues i=2002(2)2006 {

if `i'<2010 {
local num=`i'-2000
local j= "0`num'"
}

local filename_reg="state_g`j'_registration_by_g`j'_rgprec"
local filename_voters="state_g`j'_voters_by_g`j'_rgprec"
display "`filename_voters'"

import delimited "`filepath'\`filename_voters'.csv", clear case(lower)
foreach var of varlist totreg_r-oreg9g {
rename `var' v_`var'
}

save "`filepath'\voters`j'.dta", replace

import delimited "`filepath'\`filename_reg'.csv", clear case(lower)
merge m:1 rgprec_key using "`filepath'\voters`j'.dta"
drop _merge
save "`filepath'\reg_voters`j'.dta", replace

if `i'<2010 {
local crosswalk_name="state_g`j'_2011blk_by_g`j'_rg"
}

import delimited "`filepath'\`crosswalk_name'.csv", clear case(lower)
tostring fips, gen(fipstring)
capture tostring rgprec, gen(rgprecstring)
gen rgprec_key="0"+ fipstring+rgprec
capture drop fipstring rgprecstring
merge m:1 rgprec_key using "`filepath'\reg_voters`j'.dta"

drop if _merge!=3
drop _merge

save "`filepath'\`j'reg.dta", replace
erase "`filepath'\reg_voters`j'.dta"
erase "`filepath'\voters`j'.dta"
}




*We now have merged voter, registration, and crosswalk files. Convert to Census geographies. Repeat for each year in sample. 
local filepath="temp\"
clear all
use "`filepath'\02reg.dta"

*allocate registrations and voters to census block groups
drop if tract==.
foreach var of varlist totreg_r-v_oreg9g {
gen b`var'=`var'*pctrgprec/100
}


*standardize block group names "bgkey" so it's consistent and can be merged with GIS
tostring block, gen(blockstr)
tostring tract fips, replace
gen bg=substr(blockstr, 1,1)
gen tract_st=tract
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
gen bgkey="0"+fips+tract_st+bg

*convert to census block groups
collapse (sum) btotreg_r-bv_oreg9g, by(bgkey)
gen year=2002

save "`filepath'\02regbg.dta", replace


************repeat for each year in sample****************
use "`filepath'\04reg.dta"

drop if tract==.
foreach var of varlist totreg_r-v_oreg9g {
gen b`var'=`var'*pctrgprec/100
}



tostring block, gen(blockstr)
tostring tract fips, replace
gen bg=substr(blockstr, 1,1)
gen tract_st=tract
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
gen bgkey="0"+fips+tract_st+bg

collapse (sum) btotreg_r-bv_oreg9g, by(bgkey)
gen year=2004

save "`filepath'\04regbg.dta", replace
use "`filepath'\06reg.dta"


drop if tract==.
foreach var of varlist totreg_r-v_oreg9g {
gen b`var'=`var'*pctrgprec/100
}



tostring block, gen(blockstr)
tostring tract fips, replace
gen bg=substr(blockstr, 1,1)
gen tract_st=tract
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
gen bgkey="0"+fips+tract_st+bg

collapse (sum) btotreg_r-bv_oreg9g, by(bgkey)
gen year=2006

save "`filepath'\06regbg.dta", replace
use "`filepath'\08reg.dta"


drop if tract==.
foreach var of varlist totreg_r-v_oreg9g {
gen b`var'=`var'*pctsrprec/100
}



tostring block, gen(blockstr)
tostring tract fips, replace
gen bg=substr(blockstr, 1,1)
gen tract_st=tract
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
gen bgkey="0"+fips+tract_st+bg

collapse (sum) btotreg_r-bv_oreg9g, by(bgkey)
gen year=2008

save "`filepath'\08regbg.dta", replace
use "`filepath'\10reg.dta"


drop if tract==.
foreach var of varlist totreg_r-v_oreg9g {
gen b`var'=`var'*pctsrprec/100
}



tostring block, gen(blockstr)
tostring tract fips, replace
gen bg=substr(blockstr, 1,1)
gen tract_st=tract
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
gen bgkey="0"+fips+tract_st+bg

collapse (sum) btotreg_r-bv_oreg9g, by(bgkey)
gen year=2010

save "`filepath'\10regbg.dta", replace
use "`filepath'\12reg.dta"


drop if tract==.
foreach var of varlist totreg_r-v_oreg9g {
gen b`var'=`var'*pctsrprec/100
}



tostring block, gen(blockstr)
tostring tract fips, replace
gen bg=substr(blockstr, 1,1)
gen tract_st=tract
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
gen bgkey="0"+fips+tract_st+bg

collapse (sum) btotreg_r-bv_oreg9g, by(bgkey)
gen year=2012

save "`filepath'\12regbg.dta", replace
use "`filepath'\14reg.dta"


drop if tract==.
foreach var of varlist totreg_r-v_oreg9g {
gen b`var'=`var'*pctsrprec/100
}



tostring block, gen(blockstr)
tostring tract fips, replace
gen bg=substr(blockstr, 1,1)
gen tract_st=tract
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
gen bgkey="0"+fips+tract_st+bg

collapse (sum) btotreg_r-bv_oreg9g, by(bgkey)
gen year=2014

save "`filepath'\14regbg.dta", replace
use "`filepath'\16reg.dta"


drop if tract==.
foreach var of varlist totreg_r-v_oreg9g {
gen b`var'=`var'*pctsrprec/100
}



tostring block, gen(blockstr)
tostring tract fips, replace
gen bg=substr(blockstr, 1,1)
gen tract_st=tract
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
gen bgkey="0"+fips+tract_st+bg

collapse (sum) btotreg_r-bv_oreg9g, by(bgkey)
gen year=2016

save "`filepath'\16regbg.dta", replace
use "`filepath'\18reg.dta"


drop if tract==.
foreach var of varlist totreg_r-v_oreg9g {
gen b`var'=`var'*pctsrprec/100
}



tostring block, gen(blockstr)
tostring tract fips, replace
gen bg=substr(blockstr, 1,1)
gen tract_st=tract
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
gen bgkey="0"+fips+tract_st+bg

collapse (sum) btotreg_r-bv_oreg9g, by(bgkey)
gen year=2018

save "`filepath'\18regbg.dta", replace


/*************************************************************/
/*Section 2. Collect Statement of Vote files from Statewide Database,
import to STATA, import precinct to block group crosswalks from Statewide Database, use crosswalks to convert precincts to consistent Census geographies- same as the preceding 
section but this time use SoV instead of registration and voter data.
MC 11302022
*/
*************************************************************

clear all

local filepath="temp\"


forvalues i=2002(2)2018 {

if `i'<2010 {
local num=`i'-2000
local j= "0`num'"
}
if `i'>=2010 {
local num=`i'-2000
local j= "`num'"
}

local filename="state_g`j'_sov_data_by_g`j'_srprec"
display "`filename'"

import delimited "`filepath'\`filename'.csv", clear case(lower)

**need to reconstruct srprec_key for 2018 because for some reason Statewide Database didn't put it in the dataset
if `i'==2018 {
local crosswalk_name="state_g`j'_sr_blk_map"
import delimited "`filepath'\`crosswalk_name'.csv", clear case(lower)
keep county fips
duplicates drop
save "`filepath'\county_to_fips.dta", replace
import delimited "`filepath'\`filename'.csv", clear case(lower)
merge m:1 county using "`filepath'\county_to_fips.dta"
tostring fips, replace
gen srprec_key="0"+fips+srprec
drop _merge
drop fips
}

save "`filepath'\`filename'.dta", replace


if `i'>2010 {
local crosswalk_name="state_g`j'_sr_blk_map"
}

if `i'<2010 {
local crosswalk_name="state_g`j'_2011blk_by_g`j'_sr"
}

if `i'==2010 {
local crosswalk_name="state_g10_2011blk_by_g10_sr"
}
import delimited "`filepath'\`crosswalk_name'.csv", clear case(lower)


merge m:1 srprec_key using "`filepath'\`filename'.dta"

drop if _merge!=3
drop _merge

save "`filepath'\votesblocks`j'.dta", replace
}

use "`filepath'\votesblocks02.dta"

foreach var of varlist totreg-sup02 {
gen b`var'=`var'*pctsrprec/100
}


drop if tract==.
tostring block, gen(blockstr)
tostring tract fips, replace
gen bg=substr(blockstr, 1,1)
gen tract_st=tract
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
gen bgkey="0"+fips+tract_st+bg

collapse (sum) btotreg-bsup02, by(bgkey)
gen year=2002

merge 1:1 bgkey using "`filepath'\02regbg.dta"
drop _merge
save "`filepath'\bg02.dta", replace

clear all
use "`filepath'\votesblocks04.dta"


drop if election!="g04"
foreach var of varlist totreg-v101 {
gen b`var'=`var'*pctsrprec/100
}
drop if tract==.
tostring block, gen(blockstr)
tostring tract fips, replace
gen bg=substr(blockstr, 1,1)
gen tract_st=tract
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
gen bgkey="0"+fips+tract_st+bg

collapse (sum) btotreg-bv101, by(bgkey)
gen year=2004

merge 1:1 bgkey using "`filepath'\04regbg.dta"
drop _merge
save "`filepath'\bg04.dta", replace

clear all
use "`filepath'\votesblocks06.dta"



foreach var of varlist totreg-asspaf {
gen b`var'=`var'*pctsrprec/100
}

drop if tract==.
tostring block, gen(blockstr)
tostring tract fips, replace
gen bg=substr(blockstr, 1,1)
gen tract_st=tract
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
gen bgkey="0"+fips+tract_st+bg

collapse (sum) btotreg-basspaf, by(bgkey)
gen year=2006

merge 1:1 bgkey using "`filepath'\06regbg.dta"
drop _merge
save "`filepath'\bg06.dta", replace

clear all
local filepath="temp\"

use "`filepath'\votesblocks08.dta"


capture drop method
foreach var of varlist totreg-senpaf {
gen b`var'=`var'*pctsrprec/100
}

drop if tract==.
tostring block, gen(blockstr)
tostring tract fips, replace
gen bg=substr(blockstr, 1,1)
gen tract_st=tract
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
gen bgkey="0"+fips+tract_st+bg

collapse (sum) btotreg-bsenpaf, by(bgkey)
gen year=2008

merge 1:1 bgkey using "`filepath'\08regbg.dta"
drop _merge
save "`filepath'\bg08.dta", replace

clear all
use "`filepath'\votesblocks10.dta"

capture drop method
foreach var of varlist totreg-pr_27_y {
gen b`var'=`var'*pctsrprec/100
}

drop if tract==.
tostring block, gen(blockstr)
tostring tract fips, replace
gen bg=substr(blockstr, 1,1)
gen tract_st=tract
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
gen bgkey="0"+fips+tract_st+bg

collapse (sum) btotreg-bpr_27_y, by(bgkey)
gen year=2010

merge 1:1 bgkey using "`filepath'\10regbg.dta"
drop _merge
save "`filepath'\bg10.dta", replace

clear all
use "`filepath'\votesblocks12.dta"


foreach var of varlist totreg-pr_40_y {
gen b`var'=`var'*pctsrprec/100
}

drop if tract==.
tostring block, gen(blockstr)
tostring tract fips, replace
gen bg=substr(blockstr, 1,1)
gen tract_st=tract
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
gen bgkey="0"+fips+tract_st+bg

collapse (sum) btotreg-bpr_40_y, by(bgkey)
gen year=2012

merge 1:1 bgkey using "`filepath'\12regbg.dta"
drop _merge
save "`filepath'\bg12.dta", replace

clear all
use "`filepath'\votesblocks14.dta"


foreach var of varlist totreg-trsrep01 {
gen b`var'=`var'*pctsrprec/100
}

drop if tract==.
tostring block, gen(blockstr)
tostring tract fips, replace
gen bg=substr(blockstr, 1,1)
gen tract_st=tract
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
gen bgkey="0"+fips+tract_st+bg

collapse (sum) btotreg-btrsrep01, by(bgkey)
gen year=2014

merge 1:1 bgkey using "`filepath'\14regbg.dta"
drop _merge
save "`filepath'\bg14.dta", replace

clear all
use "`filepath'\votesblocks16.dta"


foreach var of varlist totreg-ussdem02 {
gen b`var'=`var'*pctsrprec/100
}

drop if tract==.
tostring block, gen(blockstr)
tostring tract fips, replace
gen bg=substr(blockstr, 1,1)
gen tract_st=tract
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
gen bgkey="0"+fips+tract_st+bg

collapse (sum) btotreg-bussdem02, by(bgkey)
gen year=2016
merge 1:1 bgkey using "`filepath'\16regbg.dta"
drop _merge
save "`filepath'\bg16.dta", replace
clear all
local filepath="temp\"
use "`filepath'\votesblocks18.dta"


foreach var of varlist totreg-ussdem02 {
gen b`var'=`var'*pctsrprec/100
}

drop if tract==.
tostring block, gen(blockstr)
tostring tract fips, replace
gen bg=substr(blockstr, 1,1)
gen tract_st=tract
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
replace tract_st="0"+tract_st if strlen(tract_st)<6
gen bgkey="0"+fips+tract_st+bg

collapse (sum) btotreg-bussdem02, by(bgkey)
gen year=2018
merge 1:1 bgkey using "`filepath'\18regbg.dta"
drop _merge
save "`filepath'\bg18.dta", replace

/*************************************************************/
/*Section 3. We now have voter data at Census block group level. This section
constructs the main independent variables- the share of ballot initiatives in 
the big government, environmental, and other liberal categories. For a list of ballot initiatives included in each category, see appendix A of the paper. 

The file shaped_incum.dta contains data on whether a house candidate is an incumbent for producing Table 5. I constructed this file by hand using records of US House races from Ballotpedia
Key:
-1 incumbent is a republican
1 incumbent is a democrat
0 no incumbent
None of the above: incumbent runs unopposed/ not a race between republican and democratic house candidates

Repeat for each year in sample. Stop reporting incumbents after shift to jungle primary (see paper)
MC 11302022
*/
*************************************************************

clear all
local filepath="C:\Users\mikec\Documents\Cal_Data\gis final\temp"
use "`filepath'\bg18.dta"

forvalues i = 1(1)8{
gen votetotal`i'=bpr_`i'_y+bpr_`i'_n
gen voteshare`i'=bpr_`i'_y/(bpr_`i'_y+bpr_`i'_n)
}
forvalues i = 10(1)12{
gen votetotal`i'=bpr_`i'_y+bpr_`i'_n
gen voteshare`i'=bpr_`i'_y/(bpr_`i'_y+bpr_`i'_n)
}


gen env_share=(voteshare1+(1-voteshare6)+voteshare12)/3
gen biggovshare=((voteshare1)+voteshare2+(voteshare3) +voteshare4+(1-voteshare5)+(1-voteshare6))/6
gen liberal_share=(voteshare8+(1-voteshare11))/2

save "`filepath'\shares18.dta", replace

clear all
use "`filepath'\bg16.dta"

forvalues i = 51(1)67{
gen votetotal`i'=bpr_`i'_y+bpr_`i'_n
gen voteshare`i'=bpr_`i'_y/(bpr_`i'_y+bpr_`i'_n)
}
gen demvote=bcngdem01
replace demvote=. if bcngdem02!=0
gen demshare=demvote/(bcngdem01+bcngrep01)


gen env_share=(voteshare56+voteshare59+voteshare67)/3
gen biggovshare=((voteshare51)+(1-voteshare53) +(voteshare55)+(voteshare61)+voteshare59)/5
gen liberal_share=(voteshare57+voteshare58+voteshare62+voteshare63+voteshare64+(1-voteshare66)+(1-voteshare54)+(1-voteshare65))/8

save "`filepath'\shares16.dta", replace

clear all
use "`filepath'\bg14.dta"


forvalues i = 1(1)2{
gen votetotal`i'=bpr_`i'_y+bpr_`i'_n
gen voteshare`i'=bpr_`i'_y/(bpr_`i'_y+bpr_`i'_n)
}
forvalues i = 45(1)48{
gen votetotal`i'=bpr_`i'_y+bpr_`i'_n
gen voteshare`i'=bpr_`i'_y/(bpr_`i'_y+bpr_`i'_n)
}
gen demvote=bcngdem01
replace demvote=. if bcngdem02!=0
gen demshare=demvote/(bcngdem01+bcngrep01)

gen env_share=voteshare1
gen biggovshare=voteshare1

gen liberal_share=(voteshare45+voteshare47+voteshare46+voteshare48)/4

save "`filepath'\shares14.dta", replace

clear all
use "`filepath'\bg12.dta"


forvalues i = 30(1)40{
gen votetotal`i'=bpr_`i'_y+bpr_`i'_n
gen voteshare`i'=bpr_`i'_y/(bpr_`i'_y+bpr_`i'_n)
}
gen demvote=bcngdem01
replace demvote=. if bcngdem02!=0
gen demshare=demvote/(bcngdem01+bcngrep01)

gen env_share=(voteshare37+voteshare39+(1-voteshare32)+(1-voteshare31))/4

gen biggovshare=(voteshare30+(1-voteshare31)+voteshare38+voteshare39)/4

gen liberal_share=(voteshare34 +voteshare36+(1-voteshare33))/3

save "`filepath'\shares12.dta", replace

clear all
use "`filepath'\bg10.dta"


forvalues i = 19(1)27{
gen votetotal`i'=bpr_`i'_y+bpr_`i'_n
gen voteshare`i'=bpr_`i'_y/(bpr_`i'_y+bpr_`i'_n)
}

merge 1:1 bgkey year using "`filepath'\shaped_incum.dta"
drop if _merge!=3
drop _merge

gen incumshare=.
gen incum_demshare=.
gen incum_repshare=.

gen demshare=bcngdem/(bcngdem+bcngrep+bcngaip+bcnggrn+bcnglib+bcngpaf)
gen repshare=bcngrep/(bcngdem+bcngrep+bcngaip+bcnggrn+bcnglib+bcngpaf)

replace incum_demshare=demshare if incum==1
replace incum_repshare=repshare if incum==-1
replace incumshare=demshare if incum==1
replace incumshare=repshare if incum==-1


replace demshare=. if incum<-1 | incum>1
replace repshare=. if incum<-1 | incum>1


gen env_share=(voteshare21+(1-voteshare20)+(1-voteshare23)+voteshare25+(1-voteshare26))/5

gen biggovshare=(voteshare21+(1-voteshare22)+(1-voteshare23)+voteshare24+(1-voteshare26)+(1-voteshare27))/6

gen liberal_share=voteshare19

save "`filepath'\shares10.dta", replace

clear all
use "`filepath'\bg08.dta"

gen votetotal1a=bpr_1a_y+bpr_1a_n
gen voteshare1a=bpr_1a_y/(bpr_1a_y+bpr_1a_n)
forvalues i = 2(1)12{
gen votetotal`i'=bpr_`i'_y+bpr_`i'_n
gen voteshare`i'=bpr_`i'_y/(bpr_`i'_y+bpr_`i'_n)
}

merge 1:1 bgkey year using "`filepath'\shaped_incum.dta"
drop if _merge!=3
drop _merge

gen incumshare=.
gen incum_demshare=.
gen incum_repshare=.

gen demshare=bcngdem/(bcngdem+bcngrep+bcngaip+bcnggrn+bcnglib+bcngpaf)
gen repshare=bcngrep/(bcngdem+bcngrep+bcngaip+bcnggrn+bcnglib+bcngpaf)

replace incum_demshare=demshare if incum==1
replace incum_repshare=repshare if incum==-1
replace incumshare=demshare if incum==1
replace incumshare=repshare if incum==-1


replace demshare=. if incum<-1 | incum>1
replace repshare=. if incum<-1 | incum>1


gen env_share=(voteshare1a+(1-voteshare7)+(1-voteshare10))/3

gen biggovshare=(voteshare1a+voteshare3+voteshare5+(voteshare6)+(1-voteshare7)+voteshare10+voteshare12)/7

gen liberal_share=(voteshare2+(1-voteshare4)+(1-voteshare8)+(1-voteshare9)+(1-voteshare11))/5

save "`filepath'\shares08.dta", replace

clear all
use "`filepath'\bg06.dta"

gen votetotal1a=bpr_1a_y+bpr_1a_n
gen voteshare1a=bpr_1a_y/(bpr_1a_y+bpr_1a_n)

gen votetotal1b=bpr_1b_y+bpr_1b_n
gen voteshare1b=bpr_1b_y/(bpr_1b_y+bpr_1b_n)

gen votetotal1c=bpr_1c_y+bpr_1c_n
gen voteshare1c=bpr_1c_y/(bpr_1c_y+bpr_1c_n)

gen votetotal1d=bpr_1d_y+bpr_1d_n
gen voteshare1d=bpr_1d_y/(bpr_1d_y+bpr_1d_n)

gen votetotal1e=bpr_1e_y+bpr_1e_n
gen voteshare1e=bpr_1e_y/(bpr_1e_y+bpr_1e_n)


forvalues i = 83(1)90{
gen votetotal`i'=bpr_`i'_y+bpr_`i'_n
gen voteshare`i'=bpr_`i'_y/(bpr_`i'_y+bpr_`i'_n)
}

merge 1:1 bgkey year using "`filepath'\shaped_incum.dta"
drop if _merge!=3
drop _merge

gen incumshare=.
gen incum_demshare=.
gen incum_repshare=.

gen demshare=bcngdem/(bcngdem+bcngrep+bcngaip+bcnggrn+bcnglib+bcngpaf)
gen repshare=bcngrep/(bcngdem+bcngrep+bcngaip+bcnggrn+bcnglib+bcngpaf)

replace incum_demshare=demshare if incum==1
replace incum_repshare=repshare if incum==-1
replace incumshare=demshare if incum==1
replace incumshare=repshare if incum==-1


replace demshare=. if incum<-1 | incum>1
replace repshare=. if incum<-1 | incum>1


gen env_share=(voteshare1c+voteshare84+voteshare87+(1-voteshare90))/4
gen biggovshare=(voteshare1b+voteshare1c+voteshare1d+voteshare1e+voteshare84+voteshare87+voteshare88+(voteshare86))/8

gen liberal_share=((1-voteshare85)+voteshare89)/2

save "`filepath'\shares06.dta", replace
clear all
use "`filepath'\bg04.dta"

gen votetotal60a=bpr_60a_y+bpr_60a_n
gen voteshare60a=bpr_60a_y/(bpr_60a_y+bpr_60a_n)

gen votetotal1a=bpr_1a_y+bpr_1a_n
gen voteshare1a=bpr_1a_y/(bpr_1a_y+bpr_1a_n)
gen votetotal59=bpr_59_y+bpr_59_n
gen voteshare59=bpr_59_y/(bpr_59_y+bpr_59_n)


forvalues i = 61(1)72{
gen votetotal`i'=bpr_`i'_y+bpr_`i'_n
gen voteshare`i'=bpr_`i'_y/(bpr_`i'_y+bpr_`i'_n)
}

merge 1:1 bgkey year using "`filepath'\shaped_incum.dta"
drop if _merge!=3
drop _merge

gen incumshare=.
gen incum_demshare=.
gen incum_repshare=.

gen demshare=bcngdem/(bcngdem+bcngrep+bcngaip+bcnggrn+bcnglib+bcngpaf)
gen repshare=bcngrep/(bcngdem+bcngrep+bcngaip+bcnggrn+bcnglib+bcngpaf)

replace incum_demshare=demshare if incum==1
replace incum_repshare=repshare if incum==-1
replace incumshare=demshare if incum==1
replace incumshare=repshare if incum==-1

replace demshare=. if incum<-1 | incum>1
replace repshare=. if incum<-1 | incum>1


gen env_share=((1-voteshare64)+(1-voteshare68))/2
gen biggovshare=(voteshare61+voteshare63+voteshare67)/3

gen liberal_share=(voteshare66+voteshare69+voteshare71+voteshare72+voteshare65)/5

save "`filepath'\shares04.dta", replace
clear all
use "`filepath'\bg02.dta"


forvalues i = 46(1)52{
gen votetotal`i'=bpr`i'y+bpr`i'n
gen voteshare`i'=bpr`i'y/(bpr`i'y+bpr`i'n)
}

merge 1:1 bgkey year using "`filepath'\shaped_incum.dta"
drop if _merge!=3
drop _merge

gen incumshare=.
gen incum_demshare=.
gen incum_repshare=.

gen demshare=bcngdem/(bcngdem+bcngrep+bcngaip+bcnggrn+bcnglib+bcngpaf)
gen repshare=bcngrep/(bcngdem+bcngrep+bcngaip+bcnggrn+bcnglib+bcngpaf)

replace incum_demshare=demshare if incum==1
replace incum_repshare=repshare if incum==-1
replace incumshare=demshare if incum==1
replace incumshare=repshare if incum==-1


replace demshare=. if incum<-1 | incum>1
replace repshare=. if incum<-1 | incum>1


gen env_share=(voteshare50+voteshare52)/2
gen biggovshare=(voteshare46+voteshare47+voteshare49+voteshare50)/4

gen liberal_share=voteshare51

save "`filepath'\shares02.dta", replace

local filepath="temp\"
clear all
use "`filepath'\shares02.dta"

**append all the years together**
append using "`filepath'\shares04.dta" "`filepath'\shares06.dta" "`filepath'\shares08.dta" "`filepath'\shares10.dta" "`filepath'\shares12.dta" "`filepath'\shares14.dta" "`filepath'\shares16.dta" "`filepath'\shares18.dta", force
keep bgkey year demshare repshare env_share biggovshare incumshare liberal_share incum bdem brep bdcl bv_totreg_r btotreg_r
gen fips=substr(bgkey,2,4)
tostring year, generate(stryear)
gen countyyear=fips+stryear


*dataset with all voting data assembled
save "`filepath'\bgrefprecincts.dta", replace

/*************************************************************/
/*Section 4. Import and clean NHGIS data (only used for Appendix B and Figure 4)
First part is code that comes with NHGIS export. 
MC 11302022
*/
*************************************************************

set more off

clear
quietly infix                   ///
  str     year       1-9        ///
  str     regiona    10-10      ///
  str     divisiona  11-11      ///
  str     state      12-35      ///
  str     statea     36-37      ///
  str     county     38-94      ///
  str     countya    95-97      ///
  str     cousuba    98-102     ///
  str     placea     103-107    ///
  str     tracta     108-113    ///
  str     blkgrpa    114-114    ///
  str     concita    115-119    ///
  str     aianhha    120-123    ///
  str     res_onlya  124-127    ///
  str     trusta     128-131    ///
  str     aitscea    132-134    ///
  str     anrca      135-139    ///
  str     cbsaa      140-144    ///
  str     csaa       145-147    ///
  str     metdiva    148-152    ///
  str     nectaa     153-157    ///
  str     cnectaa    158-160    ///
  str     nectadiva  161-165    ///
  str     uaa        166-170    ///
  str     cdcurra    171-172    ///
  str     sldua      173-175    ///
  str     sldla      176-178    ///
  str     submcda    179-183    ///
  str     sdelma     184-188    ///
  str     sdseca     189-193    ///
  str     sdunia     194-198    ///
  str     puma5a     199-203    ///
  str     bttra      204-209    ///
  str     btbga      210-210    ///
  str     name_e     211-410    ///
  double  jn9e001    411-419    ///
  double  jn9e002    420-428    ///
  double  jn9e003    429-437    ///
  double  jn9e004    438-446    ///
  double  jn9e005    447-455    ///
  double  jn9e006    456-464    ///
  double  jn9e007    465-473    ///
  double  jn9e008    474-482    ///
  double  jn9e009    483-491    ///
  double  jn9e010    492-500    ///
  double  jn9e011    501-509    ///
  double  jn9e012    510-518    ///
  double  jn9e013    519-527    ///
  double  jn9e014    528-536    ///
  double  jn9e015    537-545    ///
  double  jn9e016    546-554    ///
  double  jn9e017    555-563    ///
  double  jn9e018    564-572    ///
  double  jn9e019    573-581    ///
  double  jn9e020    582-590    ///
  double  jn9e021    591-599    ///
  double  jn9e022    600-608    ///
  double  jn9e023    609-617    ///
  double  jn9e024    618-626    ///
  double  jn9e025    627-635    ///
  double  jn9e026    636-644    ///
  double  jn9e027    645-653    ///
  double  jn9e028    654-662    ///
  double  jn9e029    663-671    ///
  double  jn9e030    672-680    ///
  double  jn9e031    681-689    ///
  double  jn9e032    690-698    ///
  double  jn9e033    699-707    ///
  double  jn9e034    708-716    ///
  double  jn9e035    717-725    ///
  double  jpbe001    726-734    ///
  double  jpbe002    735-743    ///
  double  jpbe003    744-752    ///
  double  jqbe001    753-761    ///
  double  jsde001    762-770    ///
  double  jsde002    771-779    ///
  double  jsde003    780-788    ///
  double  jsde004    789-797    ///
  double  jsde005    798-806    ///
  double  jsde006    807-815    ///
  double  jsde007    816-824    ///
  double  jsde008    825-833    ///
  double  jsde009    834-842    ///
  double  jsde010    843-851    ///
  str     name_m     852-1051   ///
  double  jn9m001    1052-1060  ///
  double  jn9m002    1061-1069  ///
  double  jn9m003    1070-1078  ///
  double  jn9m004    1079-1087  ///
  double  jn9m005    1088-1096  ///
  double  jn9m006    1097-1105  ///
  double  jn9m007    1106-1114  ///
  double  jn9m008    1115-1123  ///
  double  jn9m009    1124-1132  ///
  double  jn9m010    1133-1141  ///
  double  jn9m011    1142-1150  ///
  double  jn9m012    1151-1159  ///
  double  jn9m013    1160-1168  ///
  double  jn9m014    1169-1177  ///
  double  jn9m015    1178-1186  ///
  double  jn9m016    1187-1195  ///
  double  jn9m017    1196-1204  ///
  double  jn9m018    1205-1213  ///
  double  jn9m019    1214-1222  ///
  double  jn9m020    1223-1231  ///
  double  jn9m021    1232-1240  ///
  double  jn9m022    1241-1249  ///
  double  jn9m023    1250-1258  ///
  double  jn9m024    1259-1267  ///
  double  jn9m025    1268-1276  ///
  double  jn9m026    1277-1285  ///
  double  jn9m027    1286-1294  ///
  double  jn9m028    1295-1303  ///
  double  jn9m029    1304-1312  ///
  double  jn9m030    1313-1321  ///
  double  jn9m031    1322-1330  ///
  double  jn9m032    1331-1339  ///
  double  jn9m033    1340-1348  ///
  double  jn9m034    1349-1357  ///
  double  jn9m035    1358-1366  ///
  double  jpbm001    1367-1375  ///
  double  jpbm002    1376-1384  ///
  double  jpbm003    1385-1393  ///
  double  jqbm001    1394-1402  ///
  double  jsdm001    1403-1411  ///
  double  jsdm002    1412-1420  ///
  double  jsdm003    1421-1429  ///
  double  jsdm004    1430-1438  ///
  double  jsdm005    1439-1447  ///
  double  jsdm006    1448-1456  ///
  double  jsdm007    1457-1465  ///
  double  jsdm008    1466-1474  ///
  double  jsdm009    1475-1483  ///
  double  jsdm010    1484-1492  ///
  using `"`filepath'\nhgis0013_ds176_20105_2010_blck_grp.dat"'


format jn9e001   %9.0f
format jn9e002   %9.0f
format jn9e003   %9.0f
format jn9e004   %9.0f
format jn9e005   %9.0f
format jn9e006   %9.0f
format jn9e007   %9.0f
format jn9e008   %9.0f
format jn9e009   %9.0f
format jn9e010   %9.0f
format jn9e011   %9.0f
format jn9e012   %9.0f
format jn9e013   %9.0f
format jn9e014   %9.0f
format jn9e015   %9.0f
format jn9e016   %9.0f
format jn9e017   %9.0f
format jn9e018   %9.0f
format jn9e019   %9.0f
format jn9e020   %9.0f
format jn9e021   %9.0f
format jn9e022   %9.0f
format jn9e023   %9.0f
format jn9e024   %9.0f
format jn9e025   %9.0f
format jn9e026   %9.0f
format jn9e027   %9.0f
format jn9e028   %9.0f
format jn9e029   %9.0f
format jn9e030   %9.0f
format jn9e031   %9.0f
format jn9e032   %9.0f
format jn9e033   %9.0f
format jn9e034   %9.0f
format jn9e035   %9.0f
format jpbe001   %9.0f
format jpbe002   %9.0f
format jpbe003   %9.0f
format jqbe001   %9.0f
format jsde001   %9.0f
format jsde002   %9.0f
format jsde003   %9.0f
format jsde004   %9.0f
format jsde005   %9.0f
format jsde006   %9.0f
format jsde007   %9.0f
format jsde008   %9.0f
format jsde009   %9.0f
format jsde010   %9.0f
format jn9m001   %9.0f
format jn9m002   %9.0f
format jn9m003   %9.0f
format jn9m004   %9.0f
format jn9m005   %9.0f
format jn9m006   %9.0f
format jn9m007   %9.0f
format jn9m008   %9.0f
format jn9m009   %9.0f
format jn9m010   %9.0f
format jn9m011   %9.0f
format jn9m012   %9.0f
format jn9m013   %9.0f
format jn9m014   %9.0f
format jn9m015   %9.0f
format jn9m016   %9.0f
format jn9m017   %9.0f
format jn9m018   %9.0f
format jn9m019   %9.0f
format jn9m020   %9.0f
format jn9m021   %9.0f
format jn9m022   %9.0f
format jn9m023   %9.0f
format jn9m024   %9.0f
format jn9m025   %9.0f
format jn9m026   %9.0f
format jn9m027   %9.0f
format jn9m028   %9.0f
format jn9m029   %9.0f
format jn9m030   %9.0f
format jn9m031   %9.0f
format jn9m032   %9.0f
format jn9m033   %9.0f
format jn9m034   %9.0f
format jn9m035   %9.0f
format jpbm001   %9.0f
format jpbm002   %9.0f
format jpbm003   %9.0f
format jqbm001   %9.0f
format jsdm001   %9.0f
format jsdm002   %9.0f
format jsdm003   %9.0f
format jsdm004   %9.0f
format jsdm005   %9.0f
format jsdm006   %9.0f
format jsdm007   %9.0f
format jsdm008   %9.0f
format jsdm009   %9.0f
format jsdm010   %9.0f

label var year      `"Data File Year"'
label var regiona   `"Region Code"'
label var divisiona `"Division Code"'
label var state     `"State Name"'
label var statea    `"State Code"'
label var county    `"County Name"'
label var countya   `"County Code"'
label var cousuba   `"County Subdivision Code"'
label var placea    `"Place Code"'
label var tracta    `"Census Tract Code"'
label var blkgrpa   `"Block Group Code"'
label var concita   `"Consolidated City Code"'
label var aianhha   `"American Indian Area/Alaska Native Area/Hawaiian Home Land Code"'
label var res_onlya `"American Indian Area/Alaska Native Area (Reservation or Statistical Entity Only)"'
label var trusta    `"American Indian Reservation with Trust Lands; trust lands only Code"'
label var aitscea   `"Tribal Subdivision/Remainder Code"'
label var anrca     `"Alaska Native Regional Corporation Code"'
label var cbsaa     `"Metropolitan Statistical Area/Micropolitan Statistical Area Code"'
label var csaa      `"Combined Statistical Area Code"'
label var metdiva   `"Metropolitan Division Code"'
label var nectaa    `"New England City and Town Area Code"'
label var cnectaa   `"Combined New England City and Town Area Code"'
label var nectadiva `"New England City and Town Area Division Code"'
label var uaa       `"Urban Area Code"'
label var cdcurra   `"Congressional District (111th Congress) Code"'
label var sldua     `"State Legislative District (Upper Chamber) Code"'
label var sldla     `"State Legislative District (Lower Chamber) Code"'
label var submcda   `"Subminor Civil Division Code"'
label var sdelma    `"School District (Elementary)/Remainder Code"'
label var sdseca    `"School District (Secondary)/Remainder Code"'
label var sdunia    `"School District (Unified)/Remainder Code"'
label var puma5a    `"Public Use Microdata Sample Area (PUMA) Code"'
label var bttra     `"Tribal Census Tract Code"'
label var btbga     `"Tribal Block Group Code"'
label var name_e    `"Estimates: Area Name"'
label var jn9e001   `"Estimates: Total"'
label var jn9e002   `"Estimates: Male"'
label var jn9e003   `"Estimates: Male: No schooling completed"'
label var jn9e004   `"Estimates: Male: Nursery to 4th grade"'
label var jn9e005   `"Estimates: Male: 5th and 6th grade"'
label var jn9e006   `"Estimates: Male: 7th and 8th grade"'
label var jn9e007   `"Estimates: Male: 9th grade"'
label var jn9e008   `"Estimates: Male: 10th grade"'
label var jn9e009   `"Estimates: Male: 11th grade"'
label var jn9e010   `"Estimates: Male: 12th grade, no diploma"'
label var jn9e011   `"Estimates: Male: High school graduate, GED, or alternative"'
label var jn9e012   `"Estimates: Male: Some college, less than 1 year"'
label var jn9e013   `"Estimates: Male: Some college, 1 or more years, no degree"'
label var jn9e014   `"Estimates: Male: Associate's degree"'
label var jn9e015   `"Estimates: Male: Bachelor's degree"'
label var jn9e016   `"Estimates: Male: Master's degree"'
label var jn9e017   `"Estimates: Male: Professional school degree"'
label var jn9e018   `"Estimates: Male: Doctorate degree"'
label var jn9e019   `"Estimates: Female"'
label var jn9e020   `"Estimates: Female: No schooling completed"'
label var jn9e021   `"Estimates: Female: Nursery to 4th grade"'
label var jn9e022   `"Estimates: Female: 5th and 6th grade"'
label var jn9e023   `"Estimates: Female: 7th and 8th grade"'
label var jn9e024   `"Estimates: Female: 9th grade"'
label var jn9e025   `"Estimates: Female: 10th grade"'
label var jn9e026   `"Estimates: Female: 11th grade"'
label var jn9e027   `"Estimates: Female: 12th grade, no diploma"'
label var jn9e028   `"Estimates: Female: High school graduate, GED, or alternative"'
label var jn9e029   `"Estimates: Female: Some college, less than 1 year"'
label var jn9e030   `"Estimates: Female: Some college, 1 or more years, no degree"'
label var jn9e031   `"Estimates: Female: Associate's degree"'
label var jn9e032   `"Estimates: Female: Bachelor's degree"'
label var jn9e033   `"Estimates: Female: Master's degree"'
label var jn9e034   `"Estimates: Female: Professional school degree"'
label var jn9e035   `"Estimates: Female: Doctorate degree"'
label var jpbe001   `"Estimates: Total"'
label var jpbe002   `"Estimates: With public assistance income"'
label var jpbe003   `"Estimates: No public assistance income"'
label var jqbe001   `"Estimates: Per capita income in the past 12 months (in 2010 inflation-adjusted d"'
label var jsde001   `"Estimates: Total"'
label var jsde002   `"Estimates: Built 2005 or later"'
label var jsde003   `"Estimates: Built 2000 to 2004"'
label var jsde004   `"Estimates: Built 1990 to 1999"'
label var jsde005   `"Estimates: Built 1980 to 1989"'
label var jsde006   `"Estimates: Built 1970 to 1979"'
label var jsde007   `"Estimates: Built 1960 to 1969"'
label var jsde008   `"Estimates: Built 1950 to 1959"'
label var jsde009   `"Estimates: Built 1940 to 1949"'
label var jsde010   `"Estimates: Built 1939 or earlier"'
label var name_m    `"Margins of error: Area Name"'
label var jn9m001   `"Margins of error: Total"'
label var jn9m002   `"Margins of error: Male"'
label var jn9m003   `"Margins of error: Male: No schooling completed"'
label var jn9m004   `"Margins of error: Male: Nursery to 4th grade"'
label var jn9m005   `"Margins of error: Male: 5th and 6th grade"'
label var jn9m006   `"Margins of error: Male: 7th and 8th grade"'
label var jn9m007   `"Margins of error: Male: 9th grade"'
label var jn9m008   `"Margins of error: Male: 10th grade"'
label var jn9m009   `"Margins of error: Male: 11th grade"'
label var jn9m010   `"Margins of error: Male: 12th grade, no diploma"'
label var jn9m011   `"Margins of error: Male: High school graduate, GED, or alternative"'
label var jn9m012   `"Margins of error: Male: Some college, less than 1 year"'
label var jn9m013   `"Margins of error: Male: Some college, 1 or more years, no degree"'
label var jn9m014   `"Margins of error: Male: Associate's degree"'
label var jn9m015   `"Margins of error: Male: Bachelor's degree"'
label var jn9m016   `"Margins of error: Male: Master's degree"'
label var jn9m017   `"Margins of error: Male: Professional school degree"'
label var jn9m018   `"Margins of error: Male: Doctorate degree"'
label var jn9m019   `"Margins of error: Female"'
label var jn9m020   `"Margins of error: Female: No schooling completed"'
label var jn9m021   `"Margins of error: Female: Nursery to 4th grade"'
label var jn9m022   `"Margins of error: Female: 5th and 6th grade"'
label var jn9m023   `"Margins of error: Female: 7th and 8th grade"'
label var jn9m024   `"Margins of error: Female: 9th grade"'
label var jn9m025   `"Margins of error: Female: 10th grade"'
label var jn9m026   `"Margins of error: Female: 11th grade"'
label var jn9m027   `"Margins of error: Female: 12th grade, no diploma"'
label var jn9m028   `"Margins of error: Female: High school graduate, GED, or alternative"'
label var jn9m029   `"Margins of error: Female: Some college, less than 1 year"'
label var jn9m030   `"Margins of error: Female: Some college, 1 or more years, no degree"'
label var jn9m031   `"Margins of error: Female: Associate's degree"'
label var jn9m032   `"Margins of error: Female: Bachelor's degree"'
label var jn9m033   `"Margins of error: Female: Master's degree"'
label var jn9m034   `"Margins of error: Female: Professional school degree"'
label var jn9m035   `"Margins of error: Female: Doctorate degree"'
label var jpbm001   `"Margins of error: Total"'
label var jpbm002   `"Margins of error: With public assistance income"'
label var jpbm003   `"Margins of error: No public assistance income"'
label var jqbm001   `"Margins of error: Per capita income in the past 12 months (in 2010 inflation-adj"'
label var jsdm001   `"Margins of error: Total"'
label var jsdm002   `"Margins of error: Built 2005 or later"'
label var jsdm003   `"Margins of error: Built 2000 to 2004"'
label var jsdm004   `"Margins of error: Built 1990 to 1999"'
label var jsdm005   `"Margins of error: Built 1980 to 1989"'
label var jsdm006   `"Margins of error: Built 1970 to 1979"'
label var jsdm007   `"Margins of error: Built 1960 to 1969"'
label var jsdm008   `"Margins of error: Built 1950 to 1959"'
label var jsdm009   `"Margins of error: Built 1940 to 1949"'
label var jsdm010   `"Margins of error: Built 1939 or earlier"'

*construct relevant variables for propensity score matching
gen sharecollege10= ((jn9e015+ jn9e016+ jn9e017+ jn9e018+ jn9e032+ jn9e033+ jn9e034+ jn9e035)/( jn9e001))*100
gen share_assistance10= (jpbe002/ jpbe001)*100
rename jqbe001 per_cap10
gen share_new_house=(( jsde002+ jsde003+ jsde004)/ jsde001)*100
rename share_new_house share_new_house10

*standardize to match block group code for merging
gen bgkey=statea+countya+tracta+blkgrpa
keep bgkey share_new_house10 per_cap10 sharecollege10 share_assistance10
save "`filepath'\acs10.dta", replace


/*************************************************************/
/*Section 5. Import and clean NHGIS Census data (only used for Appendix B and Figure 4). First part is code that comes with NHGIS export. 
MC 11302022
*/
*************************************************************

set more off

clear
quietly infix                 ///
  str     year       1-4      ///
  str     regiona    5-5      ///
  str     divisiona  6-6      ///
  str     state      7-30     ///
  str     statea     31-32    ///
  str     county     33-89    ///
  str     countya    90-92    ///
  str     cousuba    93-97    ///
  str     placea     98-102   ///
  str     tracta     103-108  ///
  str     blkgrpa    109-109  ///
  str     blocka     110-113  ///
  str     concita    114-118  ///
  str     aianhha    119-122  ///
  str     res_onlya  123-126  ///
  str     trusta     127-130  ///
  str     aitscea    131-133  ///
  str     ttracta    134-139  ///
  str     tblkgrpa   140-140  ///
  str     anrca      141-145  ///
  str     cbsaa      146-150  ///
  str     metdiva    151-155  ///
  str     csaa       156-158  ///
  str     nectaa     159-163  ///
  str     nectadiva  164-168  ///
  str     cnectaa    169-171  ///
  str     uaa        172-176  ///
  str     urbrurala  177-177  ///
  str     cda        178-179  ///
  str     sldua      180-182  ///
  str     sldla      183-185  ///
  str     zcta5a     186-190  ///
  str     submcda    191-195  ///
  str     sdelma     196-200  ///
  str     sdseca     201-205  ///
  str     sdunia     206-210  ///
  str     name       211-300  ///
  str     sabinsa    301-327  ///
  double  h7v001     328-336  ///
  double  h7x001     337-345  ///
  double  h7x002     346-354  ///
  double  h7x003     355-363  ///
  double  h7x004     364-372  ///
  double  h7x005     373-381  ///
  double  h7x006     382-390  ///
  double  h7x007     391-399  ///
  double  h7x008     400-408  ///
  double  h71001     409-417  ///
  double  h71002     418-426  ///
  double  h71003     427-435  ///
  double  h71004     436-444  ///
  double  h71005     445-453  ///
  double  h71006     454-462  ///
  double  h71007     463-471  ///
  double  h71008     472-480  ///
  double  h71009     481-489  ///
  double  h71010     490-498  ///
  double  h71011     499-507  ///
  double  h71012     508-516  ///
  double  h71013     517-525  ///
  double  h71014     526-534  ///
  double  h71015     535-543  ///
  double  ifc001     544-552  ///
  using `"`filepath'\nhgis0013_ds172_2010_blck_grp.dat"'


format h7v001    %9.0f
format h7x001    %9.0f
format h7x002    %9.0f
format h7x003    %9.0f
format h7x004    %9.0f
format h7x005    %9.0f
format h7x006    %9.0f
format h7x007    %9.0f
format h7x008    %9.0f
format h71001    %9.0f
format h71002    %9.0f
format h71003    %9.0f
format h71004    %9.0f
format h71005    %9.0f
format h71006    %9.0f
format h71007    %9.0f
format h71008    %9.0f
format h71009    %9.0f
format h71010    %9.0f
format h71011    %9.0f
format h71012    %9.0f
format h71013    %9.0f
format h71014    %9.0f
format h71015    %9.0f
format ifc001    %9.0f

label var year      `"Data File Year"'
label var regiona   `"Region Code"'
label var divisiona `"Division Code"'
label var state     `"State Name"'
label var statea    `"State Code"'
label var county    `"County Name"'
label var countya   `"County Code"'
label var cousuba   `"County Subdivision Code"'
label var placea    `"Place Code"'
label var tracta    `"Census Tract Code"'
label var blkgrpa   `"Block Group Code"'
label var blocka    `"Block Code"'
label var concita   `"Consolidated City Code"'
label var aianhha   `"American Indian Area/Alaska Native Area/Hawaiian Home Land Code"'
label var res_onlya `"American Indian Reservation with Trust Lands; reservation only Code"'
label var trusta    `"American Indian Reservation with Trust Lands; trust lands only Code"'
label var aitscea   `"Tribal Subdivision/Remainder Code"'
label var ttracta   `"Tribal Census Tract Code"'
label var tblkgrpa  `"Tribal Block Group Code"'
label var anrca     `"Alaska Native Regional Corporation Code"'
label var cbsaa     `"Metropolitan Statistical Area/Micropolitan Statistical Area Code"'
label var metdiva   `"Metropolitan Division Code"'
label var csaa      `"Combined Statistical Area Code"'
label var nectaa    `"New England City and Town Area Code"'
label var nectadiva `"NECTA Division Code"'
label var cnectaa   `"Combined New England City and Town Area Code"'
label var uaa       `"Urban Area Code"'
label var urbrurala `"Urban/Rural Code"'
label var cda       `"Congressional District (111th Congress) Code"'
label var sldua     `"State Legislative District (Upper Chamber) Code"'
label var sldla     `"State Legislative District (Lower Chamber) Code"'
label var zcta5a    `"5-Digit Zip Code Tabulation Area Code"'
label var submcda   `"Subminor Civil Division Code"'
label var sdelma    `"School District (Elementary)/Remainder Code"'
label var sdseca    `"School District (Secondary)/Remainder Code"'
label var sdunia    `"School District (Unified)/Remainder Code"'
label var name      `"Area Name-Legal/Statistical Area Description (LSAD) Term-Part Indicator"'
label var sabinsa   `"School Attendance Area Code"'
label var h7v001    `"Total"'
label var h7x001    `"Total"'
label var h7x002    `"White alone"'
label var h7x003    `"Black or African American alone"'
label var h7x004    `"American Indian and Alaska Native alone"'
label var h7x005    `"Asian alone"'
label var h7x006    `"Native Hawaiian and Other Pacific Islander alone"'
label var h7x007    `"Some Other Race alone"'
label var h7x008    `"Two or More Races"'
label var h71001    `"Total races tallied"'
label var h71002    `"Not Hispanic or Latino"'
label var h71003    `"Not Hispanic or Latino: White alone or in combination with one or more other rac"'
label var h71004    `"Not Hispanic or Latino: Black or African American alone or in combination with o"'
label var h71005    `"Not Hispanic or Latino: American Indian and Alaska Native alone or in combinatio"'
label var h71006    `"Not Hispanic or Latino: Asian alone or in combination with one or more other rac"'
label var h71007    `"Not Hispanic or Latino: Native Hawaiian and Other Pacific Islander alone or in c"'
label var h71008    `"Not Hispanic or Latino: Some Other Race alone or in combination with one or more"'
label var h71009    `"Hispanic or Latino"'
label var h71010    `"Hispanic or Latino: White alone or in combination with one or more other races"'
label var h71011    `"Hispanic or Latino: Black or African American alone or in combination with one o"'
label var h71012    `"Hispanic or Latino: American Indian and Alaska Native alone or in combination wi"'
label var h71013    `"Hispanic or Latino: Asian alone or in combination with one or more other races"'
label var h71014    `"Hispanic or Latino: Native Hawaiian and Other Pacific Islander alone or in combi"'
label var h71015    `"Hispanic or Latino: Some Other Race alone or in combination with one or more oth"'
label var ifc001    `"Total"'

*standardize bgkey code for merging and rename variables
gen bgkey=statea+countya+tracta+blkgrpa
rename h7v001 population10
rename h7x002 white10
rename h7x003 black10
rename h7x005 asian10
rename h7x004 native10
rename h7x006 hawaii10
rename h7x007 other10
rename h7x008 multiracial10
rename h71009 hispanic10
rename ifc001 housingunits10

local vars "white10 black10 asian10 native10 hawaii10 other10 multiracial10 hispanic10 housingunits10"
foreach varn of local vars {
gen share_`varn'=`varn'/population10
replace share_`varn'=share_`varn'*100
}
keep bgkey-share_housingunits10
save "`filepath'\clean_census.dta", replace

/*************************************************************/
/*Section 6. Clean fire data. Run the python code first to calculate overlays between block groups and fire perimeters, and buffers around fires. This section takes the raw GIS tables, converts them to stata, standardizes block group names, and constructs independent variable "bigfire", the main variable for the paper. 
MC 11302022
*/
*************************************************************

clear

local filepath="temp\"

*loop through years for fire perimeter overlaps
forvalues i=2002(2)2020 {
local filename="big`i'"
display "`filename'"
import dbase using "`filepath'\`filename'.dbf", clear case(lower)

keep geoid10 join_count alarm_date year
gen bigfire=0
replace bigfire=1 if join_count>0


rename geoid10 bgkey
duplicates list bgkey
keep bgkey bigfire
gen year=`i'

*do 2020 differently because this is only used for future fire placebo test. 
if `i'==2020 {
rename bigfire fire2020
}
save "`filepath'\`filename'.dta", replace
}


*loop through all buffer sizes and years
local buffer_list "5 10 20"

foreach buffer of local buffer_list{

	forvalues i=2002(2)2020 {
	local filename="buffjoin`buffer'k`i'"
	display "`filename'"
	import dbase using "`filepath'\`filename'.dbf", clear case(lower)

	
	keep geoid10 join_count
	gen in_buffer`buffer'k=0
	replace in_buffer`buffer'k=1 if join_count>0
	rename geoid10 bgkey
	duplicates list bgkey
	drop join_count
	gen year=`i'
	save "`filepath'\`filename'.dta", replace
	}
}

/*************************************************************/
/*Section 7. Merge all the fire data into the voting data. 
MC 11302022
*/
*************************************************************


clear all

local filepath="C:\Users\mikec\Documents\Cal_Data\gis final\temp"

*main voter data from end of Section 3
use "`filepath'\bgrefprecincts.dta"

*merge all the perimeter overlaps
forvalues i=2002(2)2018 {
local filename="big`i'"

merge 1:1 year bgkey using "`filepath'\big`i'.dta", update
drop _merge

}

*merge all the fires
local buffer_list "5 10 20"

foreach buffer of local buffer_list{

	forvalues i=2002(2)2018 {
	local filename="buffjoin`buffer'k`i'"
	merge 1:1 year bgkey using "`filepath'\buffjoin`buffer'k`i'", update
	drop _merge
	}
}

merge m:1 bgkey using "`filepath'\big2020.dta", update
drop _merge


*merge census data and population density data
merge m:1 bgkey using "`filepath'\clean_census.dta"
drop _merge
merge m:1 bgkey using "`filepath'\acs10.dta"
drop _merge
merge m:1 bgkey using "`filepath'\popdens.dta"
drop _merge

*remove temp folder
shell rmdir temp /q /s;
*final dataset. Analysis code in the next file (see readme)
save "bgfires.dta", replace